# import pyttsx3
#
# import pyttsx3
#
# # 模块初始化
# engine = pyttsx3.init()
#
# print('准备开始语音播报...')
#
# # 设置要播报的Unicode字符串
# engine.say("暧，在")
#
# # 等待语音播报完毕
# engine.runAndWait()
import wave
import os

from PyQt5.QtCore import QSettings
from pyaudio import PyAudio, paInt16
settings = QSettings(os.path.join(os.path.dirname(os.path.abspath(__file__)), "config\\config.ini"),
                     QSettings.IniFormat)
settings.setIniCodec("UTF-8")

class WriteAudio():
    def __init__(self):
        self.p = PyAudio()
        self.framerate = settings.value("ViuceAudio/FrameRate")
        self.NUM_SAMPLES = settings.value("ViuceAudio/NUM_SAMPLES")
        self.TIME = settings.value("ViuceAudio/TIME")
        self.channels = settings.value("ViuceAudio/Channels")
        self.sampwidth = settings.value("ViuceAudio/SampWidth")
        self.CHUNK = settings.value("ViuceAudio/CHUNK")
    def stemStop(self):
        # stream.stop_stream()  # 停止数据流
        # stream.close()
        self.p.terminate()
    def record(self):
        try:
            Steam = self.p.open(format=paInt16, channels=1, rate=self.framerate, input=True,
                                frames_per_buffer=self.NUM_SAMPLES)

            my_buf = [] # 其中添加的byte类型的音频文件
            count = 0
            while count < self.TIME * 20:
                SringAudioDate = Steam.read(self.NUM_SAMPLES)
                my_buf.append(SringAudioDate)
                count += 1
                print("正在录音")
            self.writeWareFile("1.wav", my_buf)
            Steam.stop_stream()
            Steam.close()
        except Exception as f:
            print(f)

    def record2(self):
        self.stream1 = self.p.open(format=paInt16, channels=1, rate=self.framerate, input=True,
                                frames_per_buffer=self.NUM_SAMPLES)  # 打开数据流
        self.frames = []
        n = 0
        while not (self.stream1.is_stopped()):
            data = self.stream1.read(self.NUM_SAMPLES)  # 从麦克风读取数据
            self.frames.append(data)
            print(data, n)
            n = n + 1

    def playWav(self,fileName):
        '''播报语音'''
        try:

            wf = wave.open(fileName, 'rb')
            stream = self.p.open(format=self.p.get_format_from_width(wf.getsampwidth()),
                                      channels=wf.getnchannels(), rate=wf.getframerate(),
                                      output=True)  # 打开数据流--【获取音频格式信息】
            # output=True   输出的意思
            data = wf.readframes(self.CHUNK)  # 读取数据
            while data != b'' and not(stream.is_stopped()):
                stream.write(data)  #播放
                data = wf.readframes(self.CHUNK)
            stream.stop_stream()
            stream.close()
            print("结束")

        except Exception as f:
            print(f)

    def writeWareFile(self, fineName, data):
        '''写入wave信息'''
        try:
            ft = wave.open(fineName, "wb")
            ft.setnchannels(self.channels)
            ft.setframerate(self.framerate)
            ft.setsampwidth(self.sampwidth)
            ft.writeframes(b"".join(data))
            ft.close()
        except Exception as f:
            print(f)
    def recordVocie(self):
        '''播报wave语音'''
        try:
            pass
        except Exception as f:
            print(f)

    def stopv(self):
        # todo

        n = 0
        while not (self.stream1.is_stopped()):
            data = self.stream1.read(self.CHUNK)  # 从麦克风读取数据
            self.frames.append(data)
            print(data, n)
            n = n + 1



# obj = WriteAudio()
# # obj.record()
# obj.playWav()


a = [(1,2,3,4)]
ret = a.pop(0)
print(ret)
